package com.bigflexlib.etc.tree
{
	
	import com.bigflexlib.utils.BasicTestBase;


	public class TreeNodeTest extends BasicTestBase
	{


		public function test_clone( ):void
		{

			var a_1		:TreeNode = new TreeNode( );
			var a_1_1	:TreeNode = new TreeNode( );			
			
			a_1.children.addItem( a_1_1 );
			
			
			var b_1:TreeNode = a_1.clone( );
			
			assertEquals( b_1.cloneOf , a_1 );
			assertEquals( b_1.children.length , 0 )
		
		}




		public function test_deepClone( ):void
		{

			var a_1		:TreeNode = new TreeNode( );
			var a_1_1	:TreeNode = new TreeNode( );
			var a_1_2	:TreeNode = new TreeNode( );					
			var a_1_2_1	:TreeNode = new TreeNode( );	
			
			
			a_1.children.addItem( a_1_1 );
			a_1.children.addItem( a_1_2 );			
				a_1_2.children.addItem( a_1_2_1 );				
			
				
			var b_1		:TreeNode = a_1.deepClone( );
			
				assertEquals( b_1.children.length , 2 );
				
			var b_1_1	:TreeNode = b_1.children.getItemAt( 0 ) as TreeNode;
				
				assertEquals( b_1_1.cloneOf , a_1_1 );

			var b_1_2	:TreeNode = b_1.children.getItemAt( 1 ) as TreeNode;

				assertEquals( b_1_2.cloneOf , a_1_2 );				
		
			var b_1_2_1	:TreeNode = b_1_2.children.getItemAt( 0 ) as TreeNode;

				assertEquals( b_1_2_1.cloneOf , a_1_2_1 );	


		
		
			// finding clones
			
				
				assertEquals(  a_1.findCloneInOtherTree( b_1 ) , b_1  );
				assertEquals(  a_1_1.findCloneInOtherTree( b_1 ) , b_1_1  );		
				assertEquals(  a_1_2_1.findCloneInOtherTree( b_1 ) , b_1_2_1  );		
		
		
		
		
			// with stops
			
			b_1 = a_1.deepClone( [ a_1_2 ] );
			
				assertEquals( b_1.children.length , 2 );
			
			b_1_2 = b_1.children.getItemAt( 1 ) as TreeNode;
			
				assertEquals( b_1_2.children.length , 0 ); // children are not copied over because we provided a stop node
			
		
		
		
		
		}


		
		















		public function test_getIntermediateNodes( ):void
		{

			var tn1:TreeNode = new TreeNode( );
			var tn2:TreeNode = new TreeNode( );
			var tn3:TreeNode = new TreeNode( );
		
		
			var res:Array = [];
			
			// initial assertions
			
				assertNull( tn1.getIntermediateNodes( null ), null  );	// null returns null
				assertNull( tn1.getIntermediateNodes( tn1 ), null  );	// same returns null
				assertNull( tn1.getIntermediateNodes( tn2 ), null  );	// not related returns null
				

			// create a small tree
			
			tn1.children.addItem( tn2 );
			  tn2.children.addItem( tn3 );			
			
				
				// invariants
				assertNull( tn1.getIntermediateNodes( null ), null  );	// null returns null
				assertNull( tn1.getIntermediateNodes( tn1 ), null  );	// same returns null			
			
			
				
					
				assertContainersEqual( tn1.getIntermediateNodes( tn2 ), []  );
				assertContainersEqual( tn1.getIntermediateNodes( tn3 ), [ tn2 ]  );		

				// inverse direction
				assertContainersEqual( tn3.getIntermediateNodes( tn2 ), [  ]  );			
				assertContainersEqual( tn3.getIntermediateNodes( tn1 ), [ tn2 ]  );		
		

			
		
		
		}
		
		
	}
}